﻿namespace Website
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;

    public class GlobalApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            #region /page

            routes.MapRoute(
                "POST page",
                "page",
                new { controller = "Page", action = "Post" },
                new { httpMethod = new HttpMethodConstraint("POST") });

            routes.MapRoute(
                "page",
                "page",
                new { controller = "Page", action = "ContentNegotiation" });

            #endregion

            #region /page.html

            routes.MapRoute(
                "page.html",
                "page.html",
                new { controller = "Page", action = "HtmlRepresentation" });

            #endregion

            #region /page.xhtml

            routes.MapRoute(
                "page.xhtml",
                "page.xhtml",
                new { controller = "Page", action = "XhtmlRepresentation" });

            #endregion

            #region /page/{title}

            routes.MapRoute(
                "DELETE page/{title}",
                "page/{title}",
                new { controller = "PageTitle", action = "Delete", title = "" },
                new { httpMethod = new HttpMethodConstraint("DELETE") });

            routes.MapRoute(
                "PUT page/{title}",
                "page/{title}",
                new { controller = "PageTitle", action = "Put", title = "" },
                new { httpMethod = new HttpMethodConstraint("PUT") });

            routes.MapRoute(
                "page/{title}",
                "page/{title}",
                new { controller = "PageTitle", action = "ContentNegotiation" },
                new { title = @"[^\.]*" });

            #endregion

            #region /page/{title}.atom

            routes.MapRoute(
                "page/{title}.atom",
                "page/{title}.atom",
                new { controller = "PageTitle", action = "AtomRepresentation" });

            #endregion

            #region /page/{title}.txt

            routes.MapRoute(
                "page/{title}.txt",
                "page/{title}.txt",
                new { controller = "PageTitle", action = "TextRepresentation" });

            #endregion

            #region /page/{title}.html

            routes.MapRoute(
                "page/{title}.html",
                "page/{title}.html",
                new { controller = "PageTitle", action = "HtmlRepresentation" });

            #endregion

            #region /page/{title}.xhtml

            routes.MapRoute(
                "page/{title}.xhtml",
                "page/{title}.xhtml",
                new { controller = "PageTitle", action = "XhtmlRepresentation" });

            #endregion

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }
}